/*
 * Copyright (c) 2008 by 1eEurope (Switzerland) AG, Thalwil
 *
 * Customer:	1eEurope (Switzerland) AG
 * Project:		BKS
 * Created:		30.09.2008, WBL
 *
*/

using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Microsoft.SharePoint;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.WebControls;

namespace By1eEurope.BKS.Controls
{

    /// <summary>
    /// Displays a text from a SharePoint List depending on the current Language
    /// </summary>
    public class LanguageText : WebControl
    {
        private string webSiteUrl;
        /// <summary>
        /// URL to the WebSite with the List
        /// </summary>
        public string WebSiteUrl
        {
            set { this.webSiteUrl = value; }
        }

        private string listTitle;
        /// <summary>
        /// Title of the List with the LanguageTexts
        /// </summary>
        public string ListTitle
        {
            set { this.listTitle = value; }
        }

        private string columnName;
        /// <summary>
        /// Name of the key-column
        /// </summary>
        public string ColumnName
        {
            set { this.columnName = value; }
        }

        private string columnValueTrue;
        /// <summary>
        /// Value of the key-column
        /// </summary>
        public string ColumnValueTrue
        {
            set { this.columnValueTrue = value; }
        }

        private string columnValueFalse;
        /// <summary>
        /// key value (list is filtered with this value on the key-column)
        /// </summary>
        public string ColumnValueFalse
        {
            set { this.columnValueFalse = value; }
        }

        private string paramName;
        public string ParamName
        {
            set { this.paramName = value; }
        }

        private string paramValue;
        public string ParamValue
        {
            set { this.paramValue = value; }
        }
        
        private string[] langColumnNames;
        /// <summary>
        /// Column names; mapping of column names to languages is defined by the order
        /// </summary>
        public string LangColumnNames
        {
            set { this.langColumnNames = value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); }
        }

        private string[] langNames;
        /// <summary>
        /// Languages; mapping of column names to languages is defined by the order
        /// </summary>
        public string LangNames
        {
            set { this.langNames = value.Split(new char[]{','}, StringSplitOptions.RemoveEmptyEntries); }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            try
            {
                using (SPWeb web = SPControl.GetContextSite(this.Context).OpenWeb(this.webSiteUrl))
                {
                    SPList list = web.Lists[this.listTitle];

                    string compareValue;
                    if (!string.IsNullOrEmpty(this.paramName))
                    {
                        if (!string.IsNullOrEmpty(this.Page.Request.QueryString[this.paramName]) && this.Page.Request.QueryString[this.paramName] == this.paramValue)
                            compareValue = this.columnValueTrue;
                        else
                            compareValue = this.columnValueFalse;
                    }
                    else
                        compareValue = this.columnValueTrue;
                    SPQuery query = new SPQuery();
                    query.Query = "<Where>" +
                                    "<Eq><FieldRef Name='" + this.columnName + "'/><Value Type='Text'>" + compareValue + "</Value></Eq>" +
                                  "</Where>";
                    SPListItemCollection mappings = list.GetItems(query);

                    int i;
                    for (i = 0; i < this.langNames.Length; i++)
                    {
                        if (this.langNames[i] == SPControl.GetContextWeb(this.Context).Locale.Name)
                            break;
                    }

                    if (this.langColumnNames.Length > i)
                        writer.Write(mappings[0][this.langColumnNames[i]].ToString());
                }
            }
            catch (Exception)
            { }
        }
    }
}
